package com.GZHU.controller.save_info;

import com.GZHU.fisco.BlockChainClient;
import com.GZHU.entity.state_change;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.GZHU.fisco.Constant;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import com.GZHU.fisco.Constant;
@Slf4j
@RestController
@Controller
public class Save_use_info {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @GetMapping("/save_use_info")
    public void save_model_info(@RequestParam("use_id") String trade_id,
                                @RequestParam("model_id") String model_id,
                                @RequestParam("consumer_id") String consumer_id,
                                @RequestParam("start_time") String start_time,
                                @RequestParam("end_time") String end_time, HttpServletResponse response) throws IOException {
        String str=String.format("VALUES ('%s', '%s', '%s', '%s', '%s')", trade_id,model_id,consumer_id,start_time,end_time);
        String sql="insert into `use_info` (`use_id`, `model_id`, `consumer_id`, `start_time`, `end_time`)" +str;
        try {
            jdbcTemplate.execute(sql);
            log.info("data save success!");
            state_change state=new state_change();
            state.setTime(start_time);
            state.setModel_id(model_id);
            state.setOperator_id(consumer_id);
            state.setChange_type("使用");
            try {
                BlockChainClient.state_to_chain(Constant.data_upload,state);
                log.info("state change to chain success!");
            }catch (Exception e){
                log.info("state change to chain fail!");
            }
        }catch (Exception e){
            log.info("data save fail!");
        }
        response.sendRedirect("/data_save.html");
    }
}
